package com.enterprise.servlet;

import java.io.IOException;


import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;

import com.enterprise.daoImpl.AdminDaoImple;
import com.enterprise.domain.Admin;

/**
 * Servlet implementation class Admin_ChkLogin
 */
@WebServlet("/admin/Admin_ChkLogin.jsp")
public class Admin_ChkLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin_ChkLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(request.getSession().getAttribute("randCheckCode").toString().equalsIgnoreCase(request.getParameter("CheckCode").toString())){
			Admin admin=new Admin();
			admin.setUsername(request.getParameter("UserName"));
			admin.setPasswords(request.getParameter("Password"));
			if(new AdminDaoImple().getCounts(admin)>0){
				request.getSession().setAttribute("status", 1);
				request.getSession().setAttribute("loginUsername", admin.getUsername());
				response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/admin/Default.jsp");
			}else{
				request.getSession().setAttribute("message", "用户名或密码错误!");
				response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/admin/Login.jsp");
			}
			
		}else{
			request.getSession().setAttribute("message", "验证码错误！");
			response.sendRedirect( request.getSession().getServletContext().getContextPath()+"/admin/Login.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
